///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package csse3004.GD.PC;
//
//import csse3004.GD.DA.PollCreatorDA;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.mvc.SimpleFormController;
//import org.springframework.web.servlet.view.RedirectView;
//
///**
// *
// * @author Lewis Kelly 42007689
// */
//public class assignToPMController extends SimpleFormController {
//    protected final Log logger = LogFactory.getLog(getClass());
//    private PollCreatorService pollCreatorService = new PollCreatorDA();
//    
//    
//    public ModelAndView onSubmit(Object command, HttpServletRequest hsr) throws ServletException {
//        int thing = Integer.parseInt(hsr.getParameter("pid"));
//        String msg = "OH GOD IT WORKED assign!";
//        
//        assignToPM ass = (assignToPM) command;
//        int aMaster = ass.getMasterID();
//        int aPoll = thing;
//        
//        logger.info("I've Got: "+aMaster+" for master");
//        logger.info("I've Got: "+aPoll+" for poll");
//        
//        
//        pollCreatorService.assignPM(aPoll, aMaster);
//        logger.info(msg);
//        logger.info("returning assignToPM page");
//        return new ModelAndView(new RedirectView(getSuccessView()));
//    }
//    
//}

package csse3004.GD.PC;

import csse3004.GD.DA.PollCreatorDA;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class assignToPMController extends SimpleFormController {
    protected final Log logger = LogFactory.getLog(getClass());
    private PollCreatorService pollCreatorService = new PollCreatorDA();
    private assignToPM apm = new assignToPM();
    
    public ModelAndView onSubmit(Object command) throws ServletException {
        
        assignToPM ass = (assignToPM) command;
        
        int aMaster = ass.getMasterID();
        int aPoll = apm.getPollID();
        
        logger.info("I've Got: "+aMaster+" for master");
        logger.info("I've Got: "+aPoll+" for polls");
        
        
        pollCreatorService.assignPM(aPoll, aMaster);
        logger.info("hi");
        logger.info("returning assignToPM page");
        return new ModelAndView(new RedirectView(getSuccessView()));
    }
    
    @Override
    protected Object formBackingObject(HttpServletRequest hsr) throws ServletException {         
        apm.setPollID(Integer.parseInt(hsr.getParameter("pid")));
        logger.info("pid: " + apm.getPollID());
        return apm;
    }
}
